﻿using CleanArchitecture.Infrastructure.Interfaces;
using CleanArchitecture.Infrastructure.StartupConfig;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;

namespace CleanArchitecture.Web.MvcRouteConfig
{
    public partial class FrontendRouteProvider : IRouteProvider
    {
        #region Methods

        /// <summary>
        /// Register routes
        /// </summary>
        /// <param name="routeBuilder">Route builder</param>
        public void RegisterRoutes(IRouteBuilder routeBuilder)
        {
            //Access Denied
            routeBuilder.MapRoute("AccessDenied", DefaultRoutePath.AccessDenied403,/*"login/",*/
                new { controller = "Security", action = "AccessDenied" });
        }

        /// <summary>
        /// Register routes
        /// DavidLee : 2020/04/16 
        /// 此方法只为前端(Frontend)中进行路由注册，因为这个方法是后加的，已经写好的路由注册不想再更改
        /// 不要问我为什么，我懒！
        /// 而增加此方法的目的是为前端页面(Frontend)配合Column的UrlRoute及Layout实现路由动态匹态及跳转
        /// </summary>
        /// <param name="endpointRouteBuilder">Route builder</param>
        public void RegisterEndpointRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {
            string pattern = string.Empty;
            //var langservice = endpointRouteBuilder.ServiceProvider.GetRequiredService<ILanguageService>();
            //var languages = langservice.GetLanguagesAll().ToList();
            pattern = "{language}/{columntype}/{SeName}";

            endpointRouteBuilder.MapDynamicControllerRoute<FrontRouteTransformer>(pattern);
            //and default one
            endpointRouteBuilder.MapControllerRoute(
                name: "Default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            //robots.txt
            endpointRouteBuilder.MapControllerRoute("robots.txt", "robots.txt",
                new { controller = "Common", action = "RobotsTextFile" });

            //page not found
            endpointRouteBuilder.MapControllerRoute("PageNotFound", DefaultRoutePath.PageNotFound404,/*"page-not-found",*/
                new { controller = "Common", action = "PageNotFound" });

            //ServerError
            endpointRouteBuilder.MapControllerRoute("ServerErrorPage", DefaultRoutePath.ErrPage,/*"/errorpage.html",*/
                new { controller = "Common", action = "ServerErrorPage" });

            //get-captha-code
            endpointRouteBuilder.MapControllerRoute("CaptchaCode", DefaultRoutePath.CaptchaCode,
                new { controller = "Common", action = "GetCaptchaCode" });

            //Access Denied
            //endpointRouteBuilder.MapControllerRoute("AccessDenied", DefaultRoutePath.AccessDenied403,
            //    new { controller = "Security", action = "AccessDenied" });
            //home page
            endpointRouteBuilder.MapControllerRoute("HomePage", "",
                new { controller = "Home", action = "Index" });
            //Frontend Login
            endpointRouteBuilder.MapControllerRoute("FrontendLogin", "/login",
                new { controller = "Home", action = "Login" });
            //Frontend Article List
            endpointRouteBuilder.MapControllerRoute("ArticleList", "/article/list",
                new { controller = "Article", action = "List" });
            //Frontend Article Detail
            endpointRouteBuilder.MapControllerRoute("ArticleDetail", "/article/detail",
                new { controller = "Article", action = "Detail" });
            //Search page
            endpointRouteBuilder.MapControllerRoute("SearchEngine", "/search",
                new { controller = "Article", action = "SearchEngine" });
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a priority of route provider
        /// </summary>
        public int Priority
        {
            get { return 0; }
        }

        #endregion
    }
}
